﻿using System.Collections.Generic;
using Rsdn.Janus.CoreServices.Extensibility;

namespace Rsdn.Janus.Database
{
    using System;
    using System.Linq;
    using BLToolkit.Data.Linq;
    using DataModel;
    using JetBrains.Annotations;
    using SmartApp;

    [Service(typeof(IForumQueryService))]
    class ForumQueryService : IForumQueryService
    {
        private readonly IServiceProvider _serviceProvider;
        private DateTime _lastUpdateDate;

        Dictionary<int,string> forumNames = new Dictionary<int, string>();

        /// <summary>
        /// Initializes a new instance of the <see cref="ForumQueryService"/> class.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        public ForumQueryService([NotNull] IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
            FillForumNames();
        }

        private void FillForumNames()
        {
            ISharedInfoService sharedInfoService = _serviceProvider.GetRequiredService<ISharedInfoService>();
            IForumDisplayConfig forumDisplayConfig =
                sharedInfoService.GetObjValue(SharedInfoKeys.ForumDisplayConfig) as IForumDisplayConfig;

            using (IJanusDataContext db = _serviceProvider.CreateDBContext())
            {
                Table<IServerForum> table = db.GetTable<IServerForum>();
                if (forumDisplayConfig != null)
                {
                    IQueryable<ForumDescrContainer> forums =
                        table.Select(
                            f => new ForumDescrContainer(forumDisplayConfig.ShowFullForumNames ? f.Descript : f.Name, f.ID));
                    foreach (ForumDescrContainer serverForum in forums)
                    {
                        forumNames.Add(serverForum.Id, serverForum.Text);
                    }
                }
            }
        }

        /// <summary>
        /// Stores the forum last update date.
        /// </summary>
        public void StoreForumLastUpdateDate()
        {
            //if (!string.IsNullOrEmpty(Config.Instance.DbDriver))
            {
                using (IJanusDataContext db = _serviceProvider.CreateDBContext())
                {
                    if (db.IsValid())
                    {
                        //IQueryable<IForumMessage> messages = db.Messages("");
                        Table<IForumMessage> table=db.GetTable<IForumMessage>();
                        DateTime? dateTime = table.Max(m => (DateTime?)m.Date);
                        if (dateTime != null)
                        {
                            _lastUpdateDate = (DateTime) dateTime;
                        }
                    }
                }
            }
        }

        public string GetForumName(int forumId)
        {
            string ret = string.Empty;
            if (forumNames.ContainsKey(forumId))
            {
                ret = forumNames[forumId];
            }

            return ret;
        }

        /// <summary>
        /// Gets the last update date.
        /// </summary>
        /// <value>The last update date.</value>
        public DateTime LastUpdateDate
        {
            get { return _lastUpdateDate; }
        }

        private class ForumDescrContainer
        {
            private readonly int _id;
            private readonly string _text;

            public ForumDescrContainer(string text, int id)
            {
                _text = text;
                _id = id;
            }

            public int Id
            {
                get { return _id; }
            }

            public string Text
            {
                get { return _text ?? string.Empty; ; }
            }

            public override string ToString()
            {
                return Text;
            }
        }

    }
}
